// pages/rz/rz.js
var app = getApp();
let api = require("../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    file:true,
    imgArr:[],    //正面
    sc_img:[],
    mobile:''
  },
  adInputChange(e){
    
    this.setData({
      mobile: e.detail.value,    //与手机号双向绑定
    })
   
  },
  upload () {
    var that = this;
    if (that.data.imgArr&&that.data.imgArr.length < 1) {
      wx.chooseMedia({
        count: 9,
        mediaType: ['image'],
        sourceType: ['album'],
        sizeType: ['original'],
        success(result){
          // console.log(result)
          that.setData({
            imgArr:result.tempFiles
            })
        
        }
      })
       //   imgArr: that.data.imgArr.concat(res.tempFilePaths)
    } else {
      wx.showToast({
        title: '最多上传1张图片',
        icon: 'none',
        duration: 3000
      });
    }
   
  },
  uploads(){
    var that=this;
    if (that.data.sc_img&&that.data.sc_img.length < 1) {
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album'],
        sizeType: ['original'],
        success(result){
          sc_img:result.tempFiles
        }
      })
    } else {
      wx.showToast({
        title: '最多上传1张图片',
        icon: 'none',
        duration: 3000
      });
    }
  },
   // 预览图片，放大预览   正面
  preview(event) {
    
    // console.log(event.currentTarget.dataset.src.tempFilePath)
    // console.log(this.data.imgArr)
    var dataArray = [];
    let currentUrl = event.currentTarget.dataset.src.tempFilePath
    dataArray.push(currentUrl)
    // console.log(dataArray[0])
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: dataArray // 需要预览的图片http链接列表
    })
  },
  previews(event) {
    // console.log(event.currentTarget.dataset.src)
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: this.data.sc_img // 需要预览的图片http链接列表
    })
  },
  // 删除图片
  deleteImg (e) {
    var that=this;
    var index = e.currentTarget.dataset.index;
    // console.log(index)
    // const pictureArr = that.data.imgArr[index].split('/')
    // that.data.imgArr.splice(index, 1)
    that.setData({
       imgArr:[]
    })
  },
  deleteImgs (e) {
    var that=this;
    var index = e.currentTarget.dataset.index;
    const pictureArr = that.data.sc_img[index].split('/')
    that.data.imgArr.splice(index, 1)
    that.setData({
       sc_img: that.data.sc_img || []
    })
  },
  //向后端发送数据保存
  refer(){    //imgArr:result.tempFiles.tempFilePath
    var that=this;
    let token = wx.getStorageSync('token')    //从同步本地缓存中获取token
    const tempFilePaths = that.data.imgArr[0].tempFilePath
    // console.log(app.globalData.token,'999')
    wx.uploadFile({
      url: api.RealApi, //仅为示例，非真实的接口地址
      filePath: tempFilePaths,
      header: {'Authorization':'JWT'+' '+token},
      name: 'file',
      formData: {
        mobile:that.data.mobile
      },
      success (result){
        if (result.statusCode==200){
          wx.navigateBack({
            delta:1
           })
        } else{

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
onLoad(options) {
    var time=wx.getStorage({
      key:'time',
      success:function (res){
        var timestamp = Date.parse(new Date());  
        timestamp = timestamp / 1000;  
        var so=timestamp<res.data
        if(so){

        } 
        else{
          wx.clearStorage()
        } 
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})